package com.bq.gametrade.ui.activity;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.bq.gametrade.R;

import java.util.List;

public class TestActivity extends Activity implements View.OnClickListener{

    private String provider;//位置提供器
    private LocationManager locationManager;//位置服务
    private Location location;

    private LocationClient locationClient = null;
    private Button btnGetAddress;
    private TextView tvShow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        tvShow = (TextView)findViewById(R.id.tv_show);
        btnGetAddress= (Button)findViewById(R.id.btn_getAddress);
        btnGetAddress.setOnClickListener(this);
//        initGps();

        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);//获得位置服务
        provider = judgeProvider(locationManager);

        if (provider != null) {//有位置提供器的情况
            //为了压制getLastKnownLocation方法的警告
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                return;
            }
            location = locationManager.getLastKnownLocation(provider);
            if (location != null) {
                getLocation(location);//得到当前经纬度并开启线程去反向地理编码
            } else {
                tvShow.setText("暂时无法获得当前位置");
            }
        }else{//不存在位置提供器的情况

        }
    }

    /**
     * 得到当前经纬度并开启线程去反向地理编码
     */
    public void getLocation(Location location) {
        String latitude = location.getLatitude() + "";
        String longitude = location.getLongitude() + "";
        String url = "http://api.map.baidu.com/geocoder/v2/?ak=pPGNKs75nVZPloDFuppTLFO3WXebPgXg&callback=renderReverse&location=" + latitude + "," + longitude + "&output=json&pois=0";

        Log.e("wu",latitude+"   "+longitude);
//        new MyAsyncTask(url).execute();

    }

        /**
         * 判断是否有可用的内容提供器
         * @return 不存在返回null
         */
    private String judgeProvider(LocationManager locationManager) {
        List<String> prodiverlist = locationManager.getProviders(true);
        if(prodiverlist.contains(LocationManager.NETWORK_PROVIDER)){
            return LocationManager.NETWORK_PROVIDER;
        }else if(prodiverlist.contains(LocationManager.GPS_PROVIDER)) {
            return LocationManager.GPS_PROVIDER;
        }else{
            Toast.makeText(TestActivity.this,"没有可用的位置提供器",Toast.LENGTH_SHORT).show();
        }
        return null;
    }

    @Override
    public void onClick(View v) {

    }

//
//    private void initGps() {
//        try{
//            MyLocationListenner myListener = new MyLocationListenner();
//            locationClient = new LocationClient(TestActivity.this);
//            locationClient.registerLocationListener(myListener);
//            LocationClientOption option = new LocationClientOption();
//            option.setOpenGps(true);
//            option.setAddrType("all");
//            option.setCoorType("bd09ll");
//            option.setScanSpan(5000);
//            option.disableCache(true);
//            option.setPoiNumber(5);
//            option.setPoiDistance(1000);
//            option.setPoiExtraInfo(true);
//            option.setPriority(LocationClientOption.GpsFirst);
//            locationClient.setLocOption(option);
//            locationClient.start();
//        }catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//
//    private class MyLocationListenner implements BDLocationListener {
//        @Override
//        public void onReceiveLocation(BDLocation location) {
//
//            if (location == null)
//                return;
//            StringBuffer sb = new StringBuffer(256);
//            if (location.getLocType() == BDLocation.TypeGpsLocation) {
////				sb.append(location.getAddrStr());
//                sb.append(location.getCity());
//            } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
//                sb.append(location.getCity());
//            }
//            if (sb.toString() != null && sb.toString().length() > 0) {
//
//                tvShow.setText(sb.toString()+location.getAddrStr());
//            }
//
//        }
//
//        public void onReceivePoi(BDLocation poiLocation) {
//
//        }
//    }
}
